package com.csu.service.serviceImpl;

import com.csu.common.CommonResponse;
import com.csu.entity.Homework;
import com.csu.mapper.HomeworksMapper;
import com.csu.mapper.SubjectMapper;
import com.csu.mapper.UserMapper;
import com.csu.service.HomeworksService;
import com.csu.vo.HomeworkSubjectRespVO;
import com.csu.vo.SubjectRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class HomeworkServiceImpl implements HomeworksService {
    @Autowired
    HomeworksMapper homeworksMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SubjectMapper subjectMapper;

    /**
     *按学科查询作业列表
     */
    @Override
    public CommonResponse<List<HomeworkSubjectRespVO>> getHomeworkList(int subjectId, int userId) {
        List<HomeworkSubjectRespVO> homeworkSubjectRespVOList = homeworksMapper.getSubjectVOList(subjectId, userId);
        if(!homeworkSubjectRespVOList.isEmpty())
            return CommonResponse.createForSuccess("查询成功！", homeworkSubjectRespVOList);
        return CommonResponse.createForError("查询失败");
    }

    /**
     * 发布作业
     */
    @Override
    public CommonResponse<Object> postHomework(int subjectId, String name, String path, int id) {
        Timestamp now = new Timestamp(new Date().getTime());
        int rows = homeworksMapper.insertHomework(subjectId, name, path, id, now);
        if(rows != 0)
            return CommonResponse.createForSuccess("发布作业成功！", null);
        return CommonResponse.createForError("发布作业失败");
    }

    /**
     * 获取作业详情
     */
    @Override
    public CommonResponse<HomeworkSubjectRespVO> getHomework(int homeworkId) {
        HomeworkSubjectRespVO subjectVO = homeworksMapper.getSubjectVOByHomeworkId(homeworkId);
        if(subjectVO != null)
            return CommonResponse.createForSuccess("获取作业详情成功", subjectVO);
        return CommonResponse.createForError("获取作业详情失败");
    }

    /**
     * 查询自己发布的作业列表
     */
    public CommonResponse<List<HomeworkSubjectRespVO>> getSelfHomework(int id){
        List<HomeworkSubjectRespVO> subjectVOList = homeworksMapper.getSubjectVOByUserId(id);
        if(subjectVOList != null)
            return CommonResponse.createForSuccess("获取作业详情成功", subjectVOList);
        return CommonResponse.createForError("获取作业详情失败");
    }

    /**
     * 老师删除作业
     */
    public CommonResponse<Object> deleteHomework(int homeworkId, int id){
        Integer userId = homeworksMapper.getUserIdByHomeworkId(homeworkId);
        if(userId == null)
            return CommonResponse.createForError("删除作业失败，无对应的作业");
        //验证是不是本人发布的
        if(userId == id) {
            int rows = homeworksMapper.deleteHomework(homeworkId);
            if (rows != 0)
                return CommonResponse.createForSuccess("删除作业成功");
            return CommonResponse.createForError("删除作业失败");
        }
        return CommonResponse.createForError("删除作业失败，不是本人发布的作业");
    }

    /**
     *按学科查询作业列表（学生，家长）
     */
    @Override
    public CommonResponse<List<HomeworkSubjectRespVO>> getHomeworkListStudent(int subjectId, int classId) {
        Integer userId = userMapper.getUserIdByClassIdAndSubjectId(subjectId, classId);
        if(userId != null){
            List<HomeworkSubjectRespVO> homeworkList = homeworksMapper.getSubjectVOList(subjectId, userId);
            return CommonResponse.createForSuccess("获取作业列表成功", homeworkList);
        }
        return CommonResponse.createForError("获取作业列表失败");
    }

    /**
     *获取作业学科列表
     */
    @Override
    public CommonResponse<List<SubjectRespVO>> getSubjects(int classId) {
        List<SubjectRespVO> subjectRespVOList = subjectMapper.getSubjectList(classId);
        if(!subjectRespVOList.isEmpty())
            return CommonResponse.createForSuccess("SUCCESS", subjectRespVOList);
        return CommonResponse.createForError("ERROR");
    }

}
